#include "twotimersdialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>

TwoTimersDialog::TwoTimersDialog() : m_counter1(0), m_counter2(0)
{
    m_label1 = new QLabel("Timer1: 0", this);
    m_label2 = new QLabel("Timer2: 0", this);
    m_setButton = new QPushButton("set", this);
    m_stopButton = new QPushButton("stop", this);
    m_timerValue = new QLineEdit("300", this);

    m_comboBox = new QComboBox(this);
    m_comboBox -> addItem("Timer1", m_timerId1);
    m_comboBox -> addItem("Timer2", m_timerId2);

    setLayout(new QVBoxLayout);

    QHBoxLayout* hl = new QHBoxLayout();

    layout() -> addWidget(m_label1);
    layout() -> addWidget(m_label2);
    layout() -> addWidget(m_comboBox);
    hl ->  addWidget(m_timerValue);
    hl -> addWidget(m_setButton);
    layout() -> addItem(hl);
    layout() -> addWidget(m_stopButton);

    connect(m_setButton, SIGNAL(clicked()), this, SLOT(setTimer()));
    connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stopTimer()));
}


void TwoTimersDialog::timerEvent(QTimerEvent *te)
{
    if (te->timerId() == m_timerId1)
    {
        m_counter1++;
        m_label1 -> setText("Timer1: " + QString::number(m_counter1));
    } else {
        m_counter2++;
        m_label2 -> setText("Timer2: " + QString::number(m_counter2));
    }
}

void TwoTimersDialog::setTimer()
{
    if (0 == m_comboBox->currentIndex())
    {
        killTimer(m_timerId1);
        m_timerId1 = startTimer(m_timerValue->text().toInt());
    } else {
        killTimer(m_timerId2);
        m_timerId2 = startTimer(m_timerValue->text().toInt());
    }
}

void TwoTimersDialog::stopTimer()
{
    if (0 == m_comboBox->currentIndex())
    {
        killTimer(m_timerId1);
    } else {
        killTimer(m_timerId2);
    }
}
